package cate.game.gm.sub;

import cate.common.table.d.GDOperation;
import cate.game.GameBody;
import cate.game.framework.share.Shared;
import cate.game.mail.po.MailBuilder;
import cate.game.pay.msg.RolePayInitResp;
import cate.game.role.Role;
import cate.game.role.mail.Mail;
import cp.solution.business.gm.KfRebateConf;
import cp.solution.business.gm.KfRebateRecord;
import easy.java.struct.MapBox;
import easy.java.time.DateTool;
import easy.java.util.ListTool;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.HashMap;

public class KfRebateRun {

    public static KfRebateRecord.ER exe(GameBody game, MapBox mb) {
        KfRebateRecord.ER er = new KfRebateRecord.ER();
        String roleId = mb.getString("roleId");
        if(StringUtils.isBlank(roleId)) {
            er.fail("MapBox中获取不到roleId参数");
            return er;
        }
        String code = mb.getString("code");
        if(StringUtils.isBlank(code)) {
            er.fail("MapBox中获取不到code参数");
            return er;
        }
        Role role = game.role.getRole(roleId);
        if(role == null) {
            er.fail("找不到roleId="+roleId+"的角色");
            return er;
        }
        if(null == role.getPay().kfRebateLogs) {
            role.getPay().kfRebateLogs = new HashMap<>();
        } else {
            String rebLog = role.getPay().kfRebateLogs.get(code);
            if(rebLog != null) {
                er.fail("角色("+role.getBase().name+")已经领取过(编码="+code+")的客服返利了");
                return er;
            }
        }
        String gameId = role.getBase().gameId;
        String groupCode = role.getGame().config.gs.getGroupCode();
        KfRebateConf conf = matchConf(code, gameId, groupCode);
        if(conf == null) {
            er.fail("找不到匹配档位奖励配置code="+code+",gameId="+gameId+",groupCode="+groupCode);
            return er;
        }

        if(role.getPay().paySum < conf.payCents) {
            er.fail("角色("+role.getBase().name+")档位不达标！才充了"+role.getPay().paySum+"分,而该档位是"+conf.payCents+"分");
            return er;
        }

        String rebLog = ""+code+"|"+conf.currencyCode+"-"+conf.payCents + "|奖励:"+conf.rewardCnf+"|"+ DateTool.instance().format(System.currentTimeMillis());
        role.getPay().kfRebateLogs.put(code, rebLog);

        KfRebateRecord record = new KfRebateRecord();
        record.code = conf.code;
        record.payCents = conf.payCents;
        record.currencyCode = conf.currencyCode;
        record.rewardCnf = conf.rewardCnf;
        record.rewardDesc = conf.rewardDesc;
        record.roleId = roleId;
        record.roleName = role.getBase().name;
        record.srvId = game.config.srvId;
        record.gameId = gameId;
        record.groupCode = groupCode;
        record.roleLevel = role.getBase().level;
        record.roleVip = role.getBase().vip;
        record.exeTime = new Date();
        er.data = record;

        Mail mail = new MailBuilder()
                .setTitle(conf.mailTitle)
                .setContent(conf.mailContent)
                .setRewardConf(conf.rewardCnf)
                .setOperation(GDOperation.KF_REBATE)
                .setOperationSub(conf.payCents).build();
        role.getMail().add(mail);

        new RolePayInitResp(role.getPay()).sendNow(role);

        return er;
    }

    private static KfRebateConf matchConf(String code, String gameId, String groupCode) {
        for(KfRebateConf conf : Shared.conf.kfRebConfList) {
            if(!StringUtils.equals(conf.code, code)) {
                continue;
            }
            if(ListTool.isNotEmpty(conf.gameIdLimits)) {
                if(!conf.gameIdLimits.contains(gameId)) {
                    continue;
                }
            }
            if(ListTool.isNotEmpty(conf.groupCodeLimits)) {
                if(!conf.groupCodeLimits.contains(groupCode)) {
                    continue;
                }
            }
            return conf;
        }
        return null;
    }
}
